package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.entity.table.StoreFunctionSettingTableDef;
import com.dingreading.cloud.soms.entity.StoreFunctionSetting;
import com.dingreading.cloud.soms.mapper.StoreFunctionSettingMapper;
import com.dingreading.cloud.soms.service.StoreFunctionSettingService;
import com.mybatisflex.core.query.QueryCondition;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author EDY
 * @since 2025-10-13
 */
@Service
public class StoreFunctionSettingServiceImpl extends BaseServiceImpl<StoreFunctionSettingMapper, StoreFunctionSetting, StoreFunctionSettingTableDef> implements StoreFunctionSettingService {

    @Override
    protected StoreFunctionSettingTableDef getTable() {
        return StoreFunctionSettingTableDef.storeFunctionSetting;
    }

    @Override
    public Map<String, StoreFunctionSetting> getByFunctionUidIn(String storeUid, List<String> functionUids) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.storeUid.eq(storeUid))
                .and(table.funciontUid.in(functionUids));
        List<StoreFunctionSetting> storeFunctionSettings = getMapper().selectListByCondition(condition);
        if (CollectionUtils.isNotEmpty(storeFunctionSettings)) {
            return storeFunctionSettings.stream().collect(
                    (Collectors.toMap(StoreFunctionSetting::getFunciontUid, Function.identity())));
        }
        return new HashMap<>();
    }

    @Override
    public StoreFunctionSetting getByUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.storeUid.eq(storeUid))
                .and(table.funciontUid.eq(uid));
        return getMapper().selectOneByCondition(condition);
    }
}
